package com.globagames.isometric.core.view
{
	import com.globagames.isometric.core.event.OperationEvent;
	import com.globagames.isometric.core.model.IMapModule;
	import com.globagames.isometric.core.model.SimpleObject;
	import com.globagames.isometric.core.view.minimap.MiniPoint;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	
	import mx.controls.Image;
	
	public class IsoMiniMap extends DraggableBox
	{
		public var ground:Image;
		private  var _mapModule:IMapModule;
		private var onScreen:BitmapData ;
		private var offScreen:BitmapData;
		private var _isomap:IsoMap;
		public var bitmapDataForStyles:Array = new Array();
		public function set mapModule(m:IMapModule) :void {
			_mapModule = m;
			initializeDisplay();						
		}
		public function set isomap(m:IsoMap) :void {
			_isomap = m;
			//_isomap.addEventListener(IsoOperation.PLACE_ISO_OBJECT, handleEventPlacement);				
		}
		public function IsoMiniMap()
		{
			super();
			this.doubleClickEnabled = true;
			this.addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);
		}
		override protected function createChildren():void{
			super.createChildren();	
		}
		private function initializeDisplay():void {
			if( ground ) this.removeChild(ground);
			onScreen = new BitmapData(_mapModule.sizeY*2 +8, _mapModule.sizeX*2 +8,true,0x55558055);
			//offScreen = new BitmapData(onScreen.width , onScreen.height,false,0x88000000);
			var clip:Bitmap = new Bitmap(onScreen);
			ground = new Image();	
			this.width = onScreen.width;
			this.height = 	onScreen.height;	
			ground.width = onScreen.width;
			ground.height = onScreen.height;
			ground.source = clip;
			this.addChildAt(ground,0);	
			this.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
			
		}
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {			
			super.updateDisplayList(unscaledWidth, unscaledHeight);			
		}
		override protected function commitProperties():void {
			super.commitProperties();
		}	
		public function handleEventPlacement(event:OperationEvent):void {
			trace("IsoMiniMap - handleEventPlacement " + event.isoObject.identifier );
			var o:SimpleObject = event.isoObject as SimpleObject;			
			trace("x,y="+o.ox+","+o.oy);
			var mp:MiniPoint = new MiniPoint();
			if( o.style >0) {
				mp.bitmap = bitmapDataForStyles[(o.style -1)] as BitmapData;
			} 
			mp.style = o.style;
			mp.originX = ground.width - ((o.oy/32) * 2) -4;
			mp.originY = (o.ox/32) * 2;
			this.addChild(mp);
			
		}
		public function doubleClickHandler(event:MouseEvent):void {
			trace("doubleClickHandler="+mouseX +","+mouseY);						
			var ox:Number = mouseY * 16;
			var oy:Number = - 16 * (mouseX - ground.width + 4);
			trace("ox,oy="+ox+"/"+oy);
			_isomap.moveIsoMap(ox,oy);
			_isomap.invalidateProperties();
		}
		
	}	
}